/*
 * Ganesa Copyright (C) 2008 Wim Crols <twimpiex@gnx.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package be.gnx.ganesa.application.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import be.gnx.ganesa.application.event.LayerCreatedEvent;
import be.gnx.ganesa.application.event.LayerCreatedListener;
import be.gnx.ganesa.application.event.ProjectCreatedEvent;
import be.gnx.ganesa.application.event.ProjectCreatedListener;

public class Layers {

  private CTabFolder cTabFolder;

  private InfoBar infoBar;

  int height = 300;

  public Layers(InfoBar infoBar, Ganesa ganesa, Composite parent) {
    this.infoBar = infoBar;
    createLayers(infoBar, ganesa, parent);
  }

  public void createLayers(InfoBar infoBar, Ganesa ganesa, Composite parent) {
    cTabFolder = new CTabFolder(parent, SWT.TOP | SWT.FLAT | SWT.BORDER | SWT.MULTI);
    CTabItem cTabItem = new CTabItem(cTabFolder, SWT.NONE);
    cTabItem.setText("layers");

    // TODO does not work with SWTSwing
    //Image iconInformation = parent.getDisplay().getSystemImage(SWT.ICON_INFORMATION);
    //final Image image = new Image(parent.getDisplay(), new Rectangle(0, 0, 20, 20));
    //GC gc = new GC(image);
    //gc.drawImage(iconInformation, 0, 0, iconInformation.getImageData().width, iconInformation.getImageData().height, 0, 0, 20, 20);
    //gc.dispose();

    final Table layersTable = new Table(cTabFolder, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    layersTable.setLinesVisible(false);
    layersTable.setHeaderVisible(false);
    final TableColumn imageColumn = new TableColumn(layersTable, SWT.NONE);
    final TableColumn textColumn = new TableColumn(layersTable, SWT.NONE);
    imageColumn.pack();
    textColumn.pack();
    cTabItem.setControl(layersTable);
    cTabFolder.setSelection(0);

    ganesa.getApplication().addProjectCreatedListener(new ProjectCreatedListener() {
      @Override
      public void projectCreatedEventhappened(ProjectCreatedEvent event) {
        event.getProject().addLayerCreatedListener(new LayerCreatedListener() {
          @Override
          public void layerCreatedEventhappened(LayerCreatedEvent event) {
            TableItem item = new TableItem(layersTable, SWT.NONE);
            // TODO does not work with SWTSwing
            //item.setImage(0, image);
            item.setText(0, "image");
            item.setText(1, event.getLayer().getName());
            imageColumn.pack();
            textColumn.pack();
          }
        });
      }
    });

    resize();
  }

  public void resize() {
    cTabFolder.setBounds(cTabFolder.getParent().getClientArea().width - (infoBar.getWidth() - infoBar.getSashWidth()),
        0, infoBar.getWidth() - infoBar.getSashWidth(), height);
  }

}
